package cn.gz3c.uitest.view;

import static android.view.MotionEvent.ACTION_DOWN;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.widget.HorizontalScrollView;

import cn.gz3c.uitest.Util;


public class MainHorizontalScrollView extends HorizontalScrollView implements OnScaleGestureListener {
    private OnScrollCall onScrollCall;
    private DispatchTouchCall dispatchTouchCall;
    private OnScaleCall onScaleCall;
    private int d;
    private int e;
    private double oldDetector = 1.0D;
    private ScaleGestureDetector scaleGestureDetector;

    public MainHorizontalScrollView(Context var1) {
        super(var1);
        this.init();
    }

    public MainHorizontalScrollView(Context var1, AttributeSet var2) {
        super(var1, var2);
        this.init();
    }

    public MainHorizontalScrollView(Context var1, AttributeSet var2, int var3) {
        super(var1, var2, var3);
        this.init();
    }

    protected void init() {
        this.scaleGestureDetector = new ScaleGestureDetector(this.getContext(), this);
        this.setLayoutDirection(LAYOUT_DIRECTION_LTR);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (this.onScrollCall != null) {
            if (this.d == l && this.e == oldl) {
                return;
            }
            this.e = oldl;
            this.d = l;
            this.onScrollCall.onScrollChanged(l, t, oldl, oldt);
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == ACTION_DOWN && this.dispatchTouchCall != null) {
            dispatchTouchCall.onDispatchTouch();
        }
        return super.dispatchTouchEvent(motionEvent);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent var1) {
        this.scaleGestureDetector.onTouchEvent(var1);
        return var1.getPointerCount() > 1 || super.onTouchEvent(var1);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
    }

    public void setOnScrollCall(OnScrollCall onScrollCall) {
        this.onScrollCall = onScrollCall;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        if (this.onScaleCall != null) {
//            SmartLog.i("ScaleGestureDetector", com.huawei.hms.audioeditor.ui.p.onScale.onScale("onScale: ").append(var1.getScaleFactor()).toString());
            double rate = Util.divide10((double) detector.getScaleFactor(), this.oldDetector);
            this.oldDetector = detector.getScaleFactor();
//            SmartLog.i("ScaleGestureDetector", "onScale2: " + var2);
            if (rate > 1.0D) {
                rate = Util.multiply(rate - 1.0D, 3.0D) + 1.0D;
            } else {
                rate = Util.multiply(1.0D - rate, 3.0D);
                rate = 1.0D - rate;
            }
            this.onScaleCall.onScale(rate);
        }

        return false;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        this.oldDetector = 1.0D;
        if (onScaleCall != null) {
            onScaleCall.isOnScale(true);
        }
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector var1) {
        this.oldDetector = 1.0D;
        if (onScaleCall != null) {
            onScaleCall.isOnScale(false);
        }
    }

    public void setDispatchTouchCall(DispatchTouchCall dispatchTouchCall) {
        this.dispatchTouchCall = dispatchTouchCall;
    }

    public void setOnScaleCall(OnScaleCall onScaleCall) {
        this.onScaleCall = onScaleCall;
    }

    public synchronized void scrollX(double dx) {
//        SmartLog.i("MainHorizontalScrollView", com.huawei.hms.audioeditor.ui.p.a.a("dragScroll getScrollX(): ").append(this.getScrollX()).append("  offset: ").append(var1).toString());
        this.post(() -> this.scrollTo((int) ((double) this.getScrollX() + dx), this.getScrollY()));
    }

    public interface OnScaleCall {
        void isOnScale(boolean var1);

        void onScale(double var1);
    }

    public interface DispatchTouchCall {
        void onDispatchTouch();
    }

    public interface OnScrollCall {
        void onScrollChanged(int var1, int var2, int var3, int var4);
    }
}
